package com.youhome.service.service;

import com.youhome.service.mapper.CompanyTokenMapper;
import com.youhome.service.model.CompanyToken;
import com.youhome.service.model.CustomCompanyTokenDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * Created by philip on 5/19/17.
 */
@Service
public class AuthenticationCompanyTokenService implements UserDetailsService {
    @Autowired
    private CompanyTokenMapper companyTokenMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        CompanyToken companyToken = companyTokenMapper.selectByPrimaryKey(Long.valueOf(s));
        if (companyToken == null)
            throw new UsernameNotFoundException("用户不存在");
        return new CustomCompanyTokenDetails(companyToken);
    }

    public UserDetails loadUserByUsername(Long s) throws UsernameNotFoundException {
        CompanyToken companyToken = companyTokenMapper.selectByPrimaryKey(s);
        if (companyToken == null)
            throw new UsernameNotFoundException("用户不存在");
        return new CustomCompanyTokenDetails(companyToken);
    }
}
